package Datastruct.Hufffman;


//赫夫曼编码

/**
 * Im
 * <p>
 * I:1
 * m:1
 */
public class NodeCode implements Comparable<NodeCode> {
    //用来描述字母的十进制数
    public Byte data;

    public int weight;

    public NodeCode left;

    public NodeCode right;

    public NodeCode(Byte data, int weight) {
        this.data = data;
        this.weight = weight;
    }

    public NodeCode(int weight) {
        this.weight = weight;
    }

    //比较权值大小
    @Override
    public int compareTo(NodeCode o) {
        return this.weight - o.weight;
    }

    @Override
    public String toString() {
        return "NodeCode{" +
                "data=" + data +
                ", weight=" + weight +
                '}';
    }

    //前序遍历
    public void preOrder() {

        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //中序遍历
    public void inOrder() {
        if (this.left != null) {
            this.left.inOrder();
        }
        System.out.println(this);

        if (this.right != null) {
            this.right.inOrder();
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }
}
